import socket


def get_file_content(file_name):
    try:
        with open(file_name, 'rb') as f:
            data = f.read()
        return data
    except Exception as e:
        print('文件不存在')
        return None


def main():
    listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    listen_socket.bind(('127.0.0.1', 9090))

    listen_socket.listen(128)

    print('等待客户端连接...')
    client_socket, client_addr = listen_socket.accept()
    print('客户端已连接，地址为：', client_addr)

    # 接受的数据
    recv_data = client_socket.recv(1024*1024)
    file_name = recv_data.decode('utf-8')
    file_content = get_file_content(file_name)

    if file_content:
        client_socket.send(file_content)

    # 关闭数据传输socket
    client_socket.close()
    # 关闭监听socket
    listen_socket.close()


if __name__ == '__main__':
    main()
